package com.tjsinfo.tjsoa.baobiao.action;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.baobiao.bo.IHqBBSetManage;
import com.tjsinfo.tjsoa.baobiao.bo.IHqBBYhManage;
import com.tjsinfo.tjsoa.baobiao.bo.IHqJshgbManage;
import com.tjsinfo.tjsoa.baobiao.bo.IHqJshtdzManage;
import com.tjsinfo.tjsoa.baobiao.vo.THqBBYh;
import com.tjsinfo.tjsoa.baobiao.vo.THqJshgbjbqk;
import com.tjsinfo.tjsoa.common.ToolKit;

public class JshgbManageAction extends DispatchAction {
	
	private IHqBBYhManage hqBBYhManage;
	private IHqJshgbManage hqJshgbManage;

	public IHqBBYhManage getHqBBYhManage() {
		return hqBBYhManage;
	}

	public void setHqBBYhManage(IHqBBYhManage hqBBYhManage) {
		this.hqBBYhManage = hqBBYhManage;
	}	
	
	public IHqJshgbManage getHqJshgbManage() {
		return hqJshgbManage;
	}

	public void setHqJshgbManage(IHqJshgbManage hqJshgbManage) {
		this.hqJshgbManage = hqJshgbManage;
	}

	/**
	 * 获取推动组成员列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getList(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
			
			try {
				JSONArray array	 = new JSONArray();
				List<Map<String, String>> tempList = hqJshgbManage.findAll();
	 			
		 		for (Map<String, String> tempMap: tempList ){
		 			JSONObject jsObject = new JSONObject();
		 			jsObject.put("id", tempMap.get("id"));
		 			jsObject.put("sort", tempList.indexOf(tempMap)+1);
		 			jsObject.put("dwmch", tempMap.get("dwmch"));
		 			jsObject.put("xm", tempMap.get("xm"));		
		 			jsObject.put("chshny", tempMap.get("chshny"));
		 			jsObject.put("sshbsh", tempMap.get("sshbsh"));	
		 			jsObject.put("zhw", tempMap.get("zhw"));	
		 			jsObject.put("zhch", tempMap.get("zhch"));	
		 			jsObject.put("gwksshj", tempMap.get("gwksshj"));	
		 			jsObject.put("jtzhzh", tempMap.get("jtzhzh"));	
		 			jsObject.put("dwdh", tempMap.get("dwdh"));
		 			jsObject.put("zhzhdh", tempMap.get("zhzhdh"));
		 			jsObject.put("gbid", tempMap.get("gbid"));
		 			array.put(jsObject);
		 		}
	 			response.getWriter().print(array);
	 		} catch (IOException e) {
	 			// TODO Auto-generated catch block
	 			e.printStackTrace();
	 		} 
			return null;
		}	
	
/**
	 * 准备进入添加记录界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toAdd(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
			
		    int yhid = Integer.valueOf(request.getParameter("yhid"));
		    String dwmchString = hqBBYhManage.findByid(yhid).getDwmch();
		    THqJshgbjbqk tdztemp = new THqJshgbjbqk(0,yhid);
		    request.setAttribute("tdz", tdztemp);
		    request.setAttribute("dwmch", dwmchString);
		    request.setAttribute("type", "admin");
			return mapping.findForward("toUpdate");
	}	

	/**
	 * 管理员准备进入修改记录界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdate(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
			
		    int id = Integer.valueOf(request.getParameter("id"));
		    THqJshgbjbqk tdztemp = hqJshgbManage.findByid(id);
		    String dwmchString = hqBBYhManage.findByid(tdztemp.getYhid()).getDwmch();
		    request.setAttribute("tdz", tdztemp);
		    request.setAttribute("dwmch", dwmchString);
		    request.setAttribute("type", "admin");
			return mapping.findForward("toUpdate");
	}	
	
	/**
	 * 普通用户准备进入修改记录界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdateByyh(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
			
		    int yhid = Integer.valueOf(request.getParameter("yhid"));
		    List <THqJshgbjbqk> tdzList = hqJshgbManage.findByyhid(yhid);
		    if (tdzList.size()<1){
		    	THqJshgbjbqk tdztemp = new THqJshgbjbqk(0,yhid);
		    	String dwmchString = hqBBYhManage.findByid(yhid).getDwmch();
			    request.setAttribute("tdz", tdztemp);
			    request.setAttribute("dwmch", dwmchString);
		    }else{
		    	THqJshgbjbqk tdztemp = tdzList.get(0);
		    	String dwmchString = hqBBYhManage.findByid(tdztemp.getYhid()).getDwmch();
		    	request.setAttribute("tdz", tdztemp);
				request.setAttribute("dwmch",dwmchString );
		    }
		    request.setAttribute("type", "yh");
			return mapping.findForward("toUpdate");
	}	
	
	/**
	 * 保存记录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveGb(ActionMapping mapping, ActionForm form,
					HttpServletRequest request, HttpServletResponse response)throws Exception {
				
		THqJshgbjbqk gbtemp=new THqJshgbjbqk();
		
		gbtemp.setYhid(Integer.valueOf(request.getParameter("yhid")));
		gbtemp.setXm(request.getParameter("xm"));
		gbtemp.setChshny(request.getParameter("chshny"));
		gbtemp.setSshbsh(request.getParameter("sshbsh"));
		gbtemp.setZhw(request.getParameter("zhw"));
		gbtemp.setZhch(request.getParameter("zhch"));
		gbtemp.setGwkshshj(request.getParameter("gwkshshj"));
		gbtemp.setJtzhzh(request.getParameter("jtzhzh"));
		gbtemp.setDwdh(request.getParameter("dwdh"));
		gbtemp.setZhzhdh(request.getParameter("zhzhdh"));
		gbtemp.setRq(ToolKit.FormatDate(new Date()));
		String idString = request.getParameter("id");
		if ("0".equals(idString)){
			hqJshgbManage.add(gbtemp);
		}else {
			gbtemp.setId(Integer.valueOf(idString));
			hqJshgbManage.update(gbtemp);
		}
        String typeString = request.getParameter("type");
        if ("admin".equals(typeString)){
        	request.setAttribute("linkurl", "baobiao/jhshy/gblist.jsp");
        }else {
			request.setAttribute("linkurl", "tjsinfo/tjsoa/baobiao/JshTjbManage.do?method=getTjb");
		}
		request.setAttribute("message", "system.save.message");
		return mapping.findForward("success");
	}

}

